package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.SysNoticeUser;
import com.ruoyi.system.domain.SysUserMsg;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.mapper.SysNoticeUserMapper;
import com.ruoyi.system.mapper.SysUserMsgMapper;
import com.ruoyi.system.service.ISysNoticeService;

/**
 * 公告 服务层实现
 * 
 * @author ruoyi
 * @date 2018-06-25
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;
    @Autowired
    private SysNoticeUserMapper sysNoticeUserMapper;
    @Autowired
    private SysUserMsgMapper sysUserMsgMapper;

    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }
    @Override
    public List<SysNotice> selectNoticeListN(SysNotice notice)
    {
    	return noticeMapper.selectNoticeListN(notice);
    }
    
    @Override
    public int selectNoticeNoReadCount(SysNotice notice) {
    	return noticeMapper.selectNoticeNoReadCount(notice);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Transactional
    @Override
    public List<SysUserMsg> insertNotice(SysNotice notice)
    {
    	noticeMapper.insertNotice(notice);
    	String[] menuIds = notice.getMenuIds();
    	List<SysNoticeUser> lis = new ArrayList<SysNoticeUser>();
    	
    	List<SysUserMsg> aList = new ArrayList<>();
    	
    	if (lis != null ) {
    		for (String long1 : menuIds) {
    			SysNoticeUser s = new SysNoticeUser();
    			s.setNoticeId(notice.getNoticeId().intValue());
    			s.setSend(long1);
    			lis.add(s);
    			if (long1.contains("-u")) {
    				SysUserMsg sysUserMsg = new SysUserMsg();
    				sysUserMsg.setIsRead("1");
    				sysUserMsg.setNoticeId(notice.getNoticeId().intValue());
    				sysUserMsg.setUserId(new Long(long1.replace("-u", "")));
    				aList.add(sysUserMsg);
    				sysUserMsg.setCreateTime(new Date());
    				sysUserMsgMapper.insertSysUserMsg(sysUserMsg);
				}
    		}
        	
		}
    	if (lis.size() != 0) {
    		sysNoticeUserMapper.batchSysNoticeUser(lis);
		}
    	
        return aList;
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Transactional
    @Override
    public List<SysUserMsg> updateNotice(SysNotice notice)
    {
    	
    	sysNoticeUserMapper.deleteSysNoticeUserByNoticeId(notice.getNoticeId().intValue());
    	String[] menuIds = notice.getMenuIds();
    	List<SysNoticeUser> lis = new ArrayList<SysNoticeUser>();

    	List<SysUserMsg> aList = new ArrayList<>();
    	if (lis != null ) {
    		for (String long1 : menuIds) {
    			SysNoticeUser s = new SysNoticeUser();
    			s.setNoticeId(notice.getNoticeId().intValue());
    			s.setSend(long1);
    			lis.add(s);
    			
    			if (long1.contains("-u")) {
    				SysUserMsg sysUserMsg = new SysUserMsg();
    				sysUserMsg.setIsRead("1");
    				sysUserMsg.setNoticeId(notice.getNoticeId().intValue());
    				sysUserMsg.setUserId(new Long(long1.replace("-u", "")));
    				aList.add(sysUserMsg);
    				sysUserMsg.setCreateTime(new Date());
    				sysUserMsgMapper.insertSysUserMsg(sysUserMsg);
				}
    		}
        	
		}
    	if (lis.size() != 0) {
    		sysNoticeUserMapper.batchSysNoticeUser(lis);
		}
    	noticeMapper.updateNotice(notice);
    	
        return aList;
    }

    /**
     * 删除公告对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(String ids)
    {
    	String[] strArray = Convert.toStrArray(ids);
    	for (String string : strArray) {
    		sysNoticeUserMapper.deleteSysNoticeUserByNoticeId(new Integer(string));
		}
        return noticeMapper.deleteNoticeByIds(strArray);
    }
}
